/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HumanCentredSystemDesign;

import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author Martin
 */
public class Loan {
    
    
    private Date issueDate;
    private String deweyIndex;
    private String libraryMemberID;
    private int loanPeriod;
    
    public Loan(){}
    public Loan(Date _issueDate, String _deweyIndex, String _libraryMemberID, int _loanPeriod){
        this.issueDate = _issueDate;
        this.deweyIndex = _deweyIndex;
        this.libraryMemberID = _libraryMemberID;
        this.loanPeriod = _loanPeriod;
    }
    
    @Override
    public String toString(){
        return "Loan: " +issueDate +" "+ deweyIndex +" " + libraryMemberID ;
    }
    public String getDeweyIndex() {
        return this.deweyIndex;
    }
    public void setDeweyIndex(String _deweyIndex){
        this.deweyIndex = _deweyIndex;
    }
    public Date getIssueDate(){
        return this.issueDate;
    }
    public void setIssueDate(Date _issueDate){
        this.issueDate = _issueDate;
    }
    public String getLibraryMemberID(){
        return this.libraryMemberID;
    }
    public void setLibraryMemberID(String _libraryMemberID){
        this.libraryMemberID = _libraryMemberID;
    }
    public int getLoanPeriod(){
        return this.loanPeriod;
    }
    public void setLoanPeriod(int _loanPeriod){
        this.loanPeriod = _loanPeriod;
    }
    public Copy getLoanedCopy(){
        Connect myConn = new Connect();
        myConn.establishConnection();
        Copy myCopy = myConn.getItemCopiesFromDatabase(deweyIndex, "").get(0);
        return myCopy;
    }
    public Borrower getBorrower(){
        Connect myConn = new Connect();
        myConn.establishConnection();
        Borrower myBorrower = myConn.getBorrowersFromDatabase(this.libraryMemberID,"","","").get(0);
        return myBorrower;
    }
    
    
    public boolean isOverdue() {
       
       Calendar now =  Calendar.getInstance();
       Calendar issued = Calendar.getInstance();
       issued.set(issueDate.getYear()+1900, issueDate.getMonth(), issueDate.getDate());
       
       issued.add(Calendar.DAY_OF_MONTH, loanPeriod);
               
       if(now.after(issued)){
       
       return true;
       }
       else {return false;}
    }
    
    public void saveToDatabase(){
        Connect myConn = new Connect();
        myConn.establishConnection();
        myConn.InsertLoanToDatabase(this);
        myConn.closeConnection();
    }
    
    public void removeLoanFromDatabase(){
        Connect myConn = new Connect();
        myConn.establishConnection();
        myConn.DeleteLoanFromDatabase(this);
        myConn.closeConnection();
        
    }
    
    public void recall(){
        try{
        Connect myConn = new Connect();
        myConn.establishConnection();
        myConn.RecallLoanInDatabase(this);
        myConn.closeConnection();
        }
        catch(Exception err){
            System.out.println("Error while recalling Loan: "+ err.toString());
        }
    }
    
    
    public static void main(String args []) {
    }
}
